package com.xxgc.wujiaosocket.config;


import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        // 启用简单的消息代理，客户端可以订阅以 /wujiao 开头的主题
        config.enableSimpleBroker("/wujiao");
        // 设置应用的目的地前缀，客户端发送消息时需要以 /app 开头
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册一个 WebSocket 端点，客户端可以通过 /ws 连接到 WebSocket 服务，允许所有来源的跨域请求，并使用 SockJS 作为备用方案
        registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
    }
}